package com.wangsd.web.coupon.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wangsd.common.basicbean.Result;
import com.wangsd.web.MyController;
import com.wangsd.web.coupon.entity.IntegralDetail;
import com.wangsd.web.coupon.entity.Member;
import com.wangsd.web.coupon.service.IntegralDetailService;
import com.wangsd.web.coupon.service.MemberService;
import com.wangsd.web.sys.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author generator
 * @since 2019-09-25
 */
@Api(value = "member", tags = "会员管理")
@RestController
@RequestMapping("/coupon/member")
public class MemberController extends MyController {

    @Autowired
    MemberService memberService;
    @Autowired
    IntegralDetailService integralDetailService;

    @ApiOperation(value = "查询会员列表", notes = "查询会员列表", httpMethod = "GET")
    @PostMapping("/selectList/{pageNum}/{pageSize}")
    public Result selectList(@ApiParam(value = "当前页", required = true) @PathVariable Integer pageNum,
                             @ApiParam(value = "每页大小", required = true) @PathVariable Integer pageSize,
                             @RequestBody @ApiParam(name = "member", value = "传入json格式") Member member) {
        SysUser userInfo = getUserInfo();
        IPage<Member> memberIPage = memberService.selectPage(userInfo.getCode(), pageNum, pageSize, member);
        return Result.success(memberIPage);

    }

    @ApiOperation(value = "删除会员", notes = "删除会员", httpMethod = "GET", response = Result.class)
    @GetMapping("/delete/{id}")
    public Result delete(@ApiParam(value = "会员id", required = true) @PathVariable Long id) {
        boolean b = memberService.removeById(id);
        return Result.success(b);
    }

    /**
     * 清零积分
     */
    @ApiOperation(value = "清零积分", notes = "清零积分", httpMethod = "POST", response = Result.class)
    @PostMapping("/dealZeroPoints")
    public Result dealZeroPoints( @RequestBody Member member) {
        /*扣除积分*/
        if (member == null) {
            return Result.fail("参数错误！");
        }
        if (member.getCompanyId() == null) {
            return Result.fail("CompanyId参数错误！");
        }
        memberService.dealZeroPoints(member);
        return Result.success();
    }

    /**
     * 详情查询
     */
    @PostMapping("/selectDetailPage/{pageNum}/{pageSize}")
    public Result selectDetailPage(@PathVariable Integer pageNum,
                                   @PathVariable Integer pageSize,
                                   @RequestBody IntegralDetail query) {
        //分页查询
        IPage<IntegralDetail> memberIPage = integralDetailService.selectPage(query.getOpenId(),pageNum, pageSize);
        return Result.success(memberIPage);
    }
}

